/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ocr.util;

import cn.ac.iie.di.datadock.rdata.exchange.client.core.REFieldType;
import cn.ac.iie.di.ocr.handler.OCRHandler.ClientPool;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class GlobalParameters {

    static private final Logger logger = Logger.getLogger(GlobalParameters.class.getName());

    public static String nameserver = "";
    public static String pic_from_topic = "";
    public static String pic_to_topic = "";
    public static String pic_from_group = "";
    public static String TMP_GWX = "";

    public static String localpath = "";

    public static int coreThreadPoolSize = -1;
    public static int maxThreadPoolSize = -1;
    public static int blockingQueueSize = -1;

    public static int bufSize = -1;
    public static int intervaltime = -1;
    public static int timeout = -1;

    public static ArrayBlockingQueue picHandlerQueue = null;
    public static ArrayBlockingQueue picSendQueue = null;
    public static ArrayBlockingQueue<Map> mppQueue = null;
    public static ArrayBlockingQueue<Map> retryQueue = null;
    public static ArrayBlockingQueue<List> updateQueue = null;

    public static String ftpStrs = "";
    public static List<String[]> ftplist = new ArrayList<String[]>();
    public static int ftptimeout = -1;

    public static String addColName = "";
    public static String pathColName = "";

    public static HashMap<String, BlockingQueue> queue = new HashMap<>();

    public static String[] colNames = new String[]{};
    public static REFieldType[] types = null;
    public static JedisConnectionHandler connectionHandler = new JedisConnectionHandler("10.144.32.29:26379;10.144.32.30:26379", "mymaster");

    public static int MPPDBSIZE = 3;
    public static String MPPDBUSER = "";
    public static String MPPDBPASSWD = "";
    public static String MPPDBURL = "";
    public static String MPPDBDRIVER = "";
    public static String TABLE_NAME = "";
    public static String MPP_PORT = "";
    public static long GET_MQ_TIME;

//    public static ClientPool pool;
    public static void init() {
        try {
//            pool = new ClientPool();

            ConfigureUtil conf = new ConfigureUtil();

            MPPDBUSER = conf.getProperty("MPPDBUSER");
            MPPDBPASSWD = conf.getProperty("MPPDBPASSWD");
            MPPDBURL = conf.getProperty("MPPDBURL");
            MPPDBDRIVER = conf.getProperty("MPPDBDRIVER");
            MPPDBSIZE = Integer.valueOf(conf.getProperty("MPPDBSIZE"));
            TABLE_NAME = conf.getProperty("TABLE_NAME");
            MPP_PORT = conf.getProperty("MPP_PORT");

            nameserver = conf.getProperty("nameserver");
            logger.info("the nameserver is " + nameserver);
            pic_from_topic = conf.getProperty("pic_from_topic");
            logger.info("the pic_from_topic is " + pic_from_topic);
            pic_to_topic = conf.getProperty("pic_to_topic");
            logger.info("the pic_to_topic is " + pic_to_topic);
            pic_from_group = conf.getProperty("pic_from_group");
            logger.info("the pic_from_group is " + pic_from_group);
            TMP_GWX = conf.getProperty("TMP_GWX");
            GET_MQ_TIME = dateToStamp(conf.getProperty("GET_MQ_TIME"));

            //ArrayBlockingQueue大小参数
            bufSize = Integer.parseInt(conf.getProperty("bufferSize"));
            //文件路径参数
            localpath = conf.getProperty("localPath");
            //线程池参数
            coreThreadPoolSize = Integer.parseInt(conf.getProperty("coreThreadPoolSize"));
            maxThreadPoolSize = Integer.parseInt(conf.getProperty("maxThreadPoolSize"));
            blockingQueueSize = Integer.parseInt(conf.getProperty("blockingQueueSize"));

            //消费参数
            intervaltime = Integer.parseInt(conf.getProperty("intervaltime"));
            timeout = Integer.parseInt(conf.getProperty("timeout"));
            logger.info("the localpath is " + localpath + " , coreThreadPoolSize is " + coreThreadPoolSize + " , maxThreadPoolSize is " + maxThreadPoolSize + " , blockingQueueSize is " + blockingQueueSize
                    + " , intervaltime is " + intervaltime + " , timeout is " + timeout);

            //信息缓存队列
            picHandlerQueue = new ArrayBlockingQueue(bufSize);
            picSendQueue = new ArrayBlockingQueue(bufSize);
            mppQueue = new ArrayBlockingQueue(bufSize);
            retryQueue = new ArrayBlockingQueue(bufSize);
            updateQueue = new ArrayBlockingQueue(bufSize);
            logger.info("init ArrayBlockingQueue(picHandlerQueue,picSendQueue) finish, and the bufferSize is " + bufSize);
            queue.put("picHandlerQueue", picHandlerQueue);
            queue.put("picSendQueue", picSendQueue);

            ftpStrs = conf.getProperty("ftp");
            logger.info("the ftpStrs is " + ftpStrs);
            String[] ftps = ftpStrs.split(";");
            for (String ftpStr : ftps) {
                ftplist.add(ftpStr.split(","));
            }
            ftptimeout = Integer.parseInt(conf.getProperty("ftptimeout"));

            addColName = conf.getProperty("addColName");
            pathColName = conf.getProperty("pathColName");
            logger.info("the ftpfile new addColname is " + addColName + " ,and the pathColName is " + pathColName);
        } catch (Exception ex) {
            logger.error("init news-parser-netlock.properties failed,cause " + ex.getMessage(), ex);
            return;
        }
    }

    public static Long dateToStamp(String s) throws ParseException {
//        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
//        res = String.valueOf(ts);
        return ts;
    }
}
